Add convenience helpers for conditions, status, and resource names#205
Open
negz wants to merge 5 commits into
Open
Add convenience helpers for conditions, status, and resource names#205negz wants to merge 5 commits into
negz wants to merge 5 commits into
Conversation
The SDK has resource.get_condition and resource.Condition for reading conditions from observed resources, but no way to write conditions back to the response. Functions build fnv1.Condition protobuf messages by hand, mapping status strings to protobuf enums and setting the target each time. This commit adds response.set_condition, which accepts a resource.Condition — the same type returned by get_condition — and appends the corresponding fnv1.Condition to the response. Signed-off-by: Nic Cope <nicc@rk0n.org>
Composition functions often derive child resource names by combining a
parent name with a discriminator. The resulting name must be a valid DNS
label — at most 63 characters. Names built from user-supplied components
can exceed this limit, and functions end up writing ad-hoc truncation
logic.
This commit adds resource.child_name, which joins name parts, appends a
deterministic 5-character SHA-256 hash suffix for uniqueness, and
truncates the prefix to fit within the 63-character limit.
child_name("my-xr", "bucket")
# "my-xr-bucket-05ecb"
child_name("my-very-long-deployment", "us-central1-a-gpu-cluster")
# "my-very-long-deployment-us-central1-a-gpu-cluste-7e130"
Signed-off-by: Nic Cope <nicc@rk0n.org>
Functions that set status on the composite resource have to wrap their
status in {"status": ...} and call resource.update. This is a small but
universal bit of boilerplate — nearly every composition function writes
status.
This commit adds resource.update_status, which accepts a dict or
Pydantic model and nests it under the status key.
Signed-off-by: Nic Cope <nicc@rk0n.org>
This commit renames set_condition to set_conditions and changes the signature to accept variadic resource.Condition arguments. Functions that set multiple conditions can now pass them all in one call. Signed-off-by: Nic Cope <nicc@rk0n.org>
When message is empty, omitting the field from the protobuf message is cleaner than explicitly setting it to an empty string. Protobuf includes explicitly-set zero values in serialization, so setting message="" causes it to appear in MessageToDict output and gRPC responses even when there is nothing to say. Signed-off-by: Nic Cope <nicc@rk0n.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description of your changes
A few small convenience helpers I've wanted across several composition functions I'm working on.
response.set_conditionaccepts aresource.Condition— the same type returned byget_condition— and appends the correspondingfnv1.Conditionto the response. The SDK had read support for conditions viaget_conditionbut no write support; functions had to buildfnv1.Conditionprotobuf messages by hand.resource.child_namebuilds deterministic, DNS-label-safe names for child resources. Composition functions that derive child names from parent names hit the 63-character DNS label limit; this encodes the truncate-and-hash pattern once.resource.update_statusnests a dict or Pydantic model under thestatuskey and callsresource.update. Nearly every composition function writes status, and the wrapping is universal boilerplate.I have: